home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / GraphicsWorkshop / Source / ImageControl.h < prev    next >
Text File  |  1992-05-25  |  9KB  |  216 lines

  1. /*
  2.  * Image Control
  3.  * ------------------
  4.  * Alex Raftis
  5.  * Sr. Project
  6.  * CalPoly San Luis Obispo
  7.  *
  8.  * Version: 0.9 (alpha)
  9.  * Last Modified: 10/10/91
  10.  *
  11.  * This package is a set of utilities for manipulating images stored in NXBitmapImageRep.
  12.  * It is linked into the control object, NXBitmapImageRepControl at initialization time, so
  13.  * you may modify it at any time. To be useful, however, it must fully implement all functions.
  14.  * The flexibility of run time linking is added so that a user has some choice over the various
  15.  * algorhythms used, and may replace them if need be. The object code should appear in
  16.  * some Library directory, either ~/Library/Converters, /LocalLibrary/Converters, or
  17.  * /NextLibrary/Converters. The libraries will be searched in that order. 
  18.  *
  19.  * Revision History:
  20.  *    5-25-92    I think I fixed the shearing problem for get next pixel functions. This will need
  21.  *            some more serious testing, and I have not yet fixed the get xy functions will will
  22.  *            still produce the shearing effect.
  23.  */
  24.  
  25. #ifndef __IMAGE__
  26. #define __IMAGE__
  27.  
  28. #import <objc/Object.h>
  29.  
  30. /* 
  31.  * Color Space defines. Mostly useless. You should use defines in graphics.h for
  32.  * the varios NXColorSpaces. Also, methods to the NXBitmapImageRep help to
  33.  * remove the need for these, as the supplied methods can give this information.
  34.  */
  35. #define IMAGE_BW            1
  36. #define IMAGE_RGB            2
  37. #define IMAGE_CYMK        3
  38. #define IMAGE_NOALPHA    0
  39. #define IMAGE_ALPHA        1
  40.  
  41. /*
  42.  * Macros that give various data from the photometric interpretation field of
  43.  * of tiff image. Mostly useless since the NXBitmapImageRep can supply this
  44.  * information.
  45.  */
  46. #define COLOR(i1)                     (i1 & 0x3)
  47. #define ALPHA(i1)                     ((i1 & 0x4) >> 2)
  48. #define PALETTE(i1)                    ((i1 & 0x8) >> 3)
  49. #define IMAGE_HASALPHA(alpha)     (ALPHA(alpha) == IMAGE_ALPHA)
  50.  
  51. /*
  52.  * Macro that converts an RGB image to gray scale, of the same magnitude at the
  53.  * original RGB values. Ie, if the max RGB values are 15, the max gray scale (white)
  54.  * will be 15.
  55.  */
  56. #define MONO(rd,gn,bl) (((rd)*11 + (gn)*16 + (bl)*5) >> 5)  /*.33R+ .5G+ .17B*/
  57.  
  58. typedef unsigned long    pixel;
  59.  
  60. /*
  61.  * Pixel
  62.  * ------
  63.  * Contains the data associated with a pixel. The count is the same as samples per
  64.  * pixels. The values in values will be the following when the following circumstance
  65.  * are true.
  66.  *     1. Black and White image-    values[1] gray scale 1 is white depending on the current
  67.  *                            color space. Message NXBitmapImageRep to be sure.
  68.  *                            values[2] contains alpha, if present.
  69.  *     2. RGB color image-        values[0] = Red, values[1] = Green, values[2] = Blue.
  70.  *                            values[4] = alpha, if present.
  71.  *    3. CMYK color image-        values[0] = Cyan, values[1] = Magenta, values[2] = Yellow,
  72.  *                            values[3] = Black. values[4] = alpha if present.
  73.  * The max value of each values[] is equal to 2 ^ BitsPerSample - 1. You should always
  74.  * querry the orignal NXBitmapImageRep to be sure of the color space for the given image,
  75.  * and thus the values of the array.
  76.  */ 
  77. typedef struct {
  78.     pixel    values[5];
  79.     int         count;
  80. } Pixel;
  81.  
  82. /*
  83.  * Pointer to a function that accepts and integer (x,y) pair, and returns a Pixel structure
  84.  * containing the pixel found and (x,y). This does not check bounds!
  85.  */
  86. typedef     Pixel * (*GetPixelXYFunc)(int, int);
  87. /* 
  88.  * Pointer to a fucntion that returns successive pixels in an image. Returns pointers to 
  89.  * a Pixel structure containing the pixel values.
  90.  */
  91. typedef     Pixel * (*GetPixelNextFunc)(void);
  92. /*
  93.  * A pointer to a function that converts a pixel structure (pointer to) to a gray scale
  94.  * value. This value should be from 0 to 2^BitsPerSample-1 of the original image.
  95.  */
  96. typedef    Pixel * (*ToGrayFunc)(Pixel *);
  97. /*
  98.  * A pointer to a function that converts a pixel structure (pointer to) to a RGB pixel
  99.  * structure. This values returned should be from 0 to 2^BitsPerSample-1 of the original image.
  100.  */
  101. typedef    Pixel * (*ToRGBFunc)(Pixel *);
  102.  
  103. @interface ImageControl: Object
  104. {
  105.      /* 
  106.      * Note: There's no class variables since c functions need access to them, and
  107.      * objective C does not support the concept of friend fucntions. However, by 
  108.      * making locally global variables within the context of the implementation module,
  109.      * I can simulate this effect. Not as nice of programming, but oh-well.
  110.      */ 
  111. }
  112.  
  113. /*
  114.  * Initializes the object. This must be called before any other method.
  115.  * Assumes:     imageIn is an NXBitmapImageRep, or will at least responds to it's messages.
  116.  * Returns:     A new instance of ImageControl.
  117.  */
  118. + new: (id)imageIn;
  119.  
  120. /*
  121.  * Returns a pointer to a C function call that will get a random pixel (x,y) from an NXBitmap-
  122.  * ImageRep. The function can then be called getting passed an (x,y) coordinate pair. The
  123.  * only limitation is that the (x,y) pair must fall with in the domain of the image. It returns 
  124.  * a pointer to a pixel structure. This structure will contain up to five values representing the
  125.  * pixel. This can then be passed to other routines like PixelToGray.
  126.  *
  127.  * Assumes:     Object initialized.
  128.  * Returns:    A Pointer to a fucntion of type GetPixelXYFunc.
  129.  */
  130. - (GetPixelXYFunc)getXYFunction;
  131.  
  132. /*
  133.  * Resets the get next pixel function to point to the pixel in the top left hand corner of the
  134.  * image. Note that this is different than normal postscript images which begin with (0,0) 
  135.  * in the lower left hand corner.
  136.  *
  137.  * Assumes:    Object initialized.
  138.  * Returns:    self.
  139.  * Results:    Object ready to accept first call to GetNextFunction (see below).
  140.  */
  141. - resetNext;
  142.  
  143. /*
  144.  * Returns a pointer to the correct get next function depending on the image type. This
  145.  * function, when call after resetNext, will eventually supply all pixels in the image. It's
  146.  * very useful for converting images. A sample algorhythms would be to:
  147.  *     1. resetNext. 
  148.  *     2. Call routine width * height times getting a pointer to a pixel. 
  149.  *    3. Call the correct conversion routine for the type of output you will be doing (Ie, 
  150.  *        RGB or BW). 
  151.  *    4. Write the correct pixel type out to the image your saving. 
  152.  * 
  153.  * Assumes:    Object initialized and resetNext has been called, if you wish to start from
  154.  *            the beginning of the program. Also, the programmer must take care to run
  155.  *            past the end of the picture, or unexpected results will occur.
  156.  * Returns:    Returns a pointer to a Pixel structure.
  157.  */
  158. - (GetPixelNextFunc)getNextFunction;
  159.  
  160. /*
  161.  * Returns a pointer to a function that converts pixel values to gray scale, where high
  162.  * values represent white.
  163.  *
  164.  * Assumes:    Object initialized.
  165.  * Returns:    Pointer to a function of type ToGrayFunc.
  166.  */
  167. - (ToGrayFunc) getToGrayFunction;
  168.  
  169. /*
  170.  * Returns a pointer to a function that converts pixel values to rgb values, where high
  171.  * values represent full intensity.
  172.  *
  173.  * Assumes:    Object initialized.
  174.  * Returns:    Pointer to a function of type ToRGBFunc.
  175.  */
  176. - (ToRGBFunc) getToRGBFunction;
  177.  
  178. /*
  179.  * Applies a Floyd Sternberg Ditherizing algorhythm to the image and return a new
  180.  * NXBitmapImageRep of a one bit image, where 1 is white.
  181.  * 
  182.  * Assumes:    Object initialized.
  183.  * Returns:    A NXBitmapImageRep (1 = BPP, 1 = SPP, CS = NX_OneIsWhiteColorSpace.
  184.  */
  185. - ditherImage;
  186.  
  187. /*
  188.  * A most knarly and nasty function, this takes the original data and conerts into a picture
  189.  * with a palette of specified size. Currently, it's implemented via a simple algorhtym that
  190.  * contructs a generic palette with a sample color from the full spectrum. This works well
  191.  * on pictures with highly contrast, varied colors, but pretty much sucks other wise. It
  192.  * get worse with the smaller the palette size. It should ideally implement a Medim-Cut
  193.  * Algorhythm, but I couldn't find the references on a good implementation, so I just hacked
  194.  * (read borrowed and modified) this piece of code from XV, an image view for X. It works,
  195.  * but produces images of questionable quality. Note, however, that if the image is black
  196.  * and white, a simple conversion is done on the data to make it matched the specified 
  197.  * palette size. Likewise, should the image contain less than the palette size in colors, 
  198.  * a simple conversion is done to preserve the original color set.
  199.  *
  200.  * Assumes:     Object initialized. size is the size of the requested palette. It should be
  201.  *            be from 8 to 256, due to limitations of the algorhythm. outPic is
  202.  *            a pointer to a pointer that will hold the final image. Space will be allocated 
  203.  *            for the image. r, g, and b are pointers to an array that can hold size bytes
  204.  *            (unsigned) char worth of data. This will envtually be the palette. This 
  205.  *            should never be used to construct palettes greater than 256 colors.
  206.  * Returns:    self
  207.  * Results:    outPic contains width * height palette entries into arrays r,g, and b.
  208.  */
  209. - convertToPalette: (int)size 
  210.         andReturn: (unsigned char **)outPic 
  211.         andPalettes: (unsigned char *)r : (unsigned char *)g : (unsigned char *)b;
  212.  
  213. @end
  214.  
  215. #endif
  216.